/**
 * @Author：ming
 * @Date：2023/10/29 2:43 PM
 * @Filename：Hot2
 */

/**
 * @author ming
 */
public class Hot2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1==null){
            return l2;
        }
        if(l2==null){
            return l1;
        }
        int res =0;
        ListNode head = new ListNode();
        head.next=null;
        ListNode p = head;
        while (l1!=null||l2!=null){
            int x = l1==null?0:l1.val;
            int y = l2==null?0:l2.val;
            if(l1!=null){
                l1=l1.next;
            }
            if(l2!=null){
                l2=l2.next;
            }
            int sum = x+y+res;
            int temp = sum%10;
            ListNode listNode = new ListNode(temp);
            p.next = listNode;
            p=p.next;
            res = sum/10;
        }
        if(res!=0){
            ListNode listNode = new ListNode(res);
            p.next = listNode;
            p=p.next;
        }

        return head.next;
    }
}
